Drug Overdose Deaths in the US County Level

Code
#load full dataset
full_data <- read_csv("data/joined_data.csv")
Code
#create new column of overdose deaths per capita
full_data2 <- full_data %>%
    mutate(Deaths_per_capita = (mean_addiction_deaths/census_2020_pop))

#log transform Deaths_per_capita
full_data2 <- full_data2 %>%
    mutate(Log_deaths_per_capita = ifelse(Deaths_per_capita > 0, log(Deaths_per_capita), NA))

#create column for text tooltip 
full_data2 <- full_data2 %>%
    mutate(log_county_hover_text = paste0( 
    "County: ", county_name, "<br>",
    "County FIP: ", fips, "<br>",
    "Average Deaths per Capita: ", Deaths_per_capita))
Code
#create choropleth map with log transformed overdose deaths per capita

#source code site: https://plotly.com/r/choropleth-maps/
url <- 'https://raw.githubusercontent.com/plotly/datasets/master/geojson-counties-fips.json'
counties <- rjson::fromJSON(file=url)

g <- list( 
    scope = 'usa',
    projection = list(type = 'albers usa'),
    showlakes = TRUE,
    lakecolor = toRGB('white')
)

fig <- plot_ly(
        type="choropleth",
        geojson=counties,
        locations=full_data2$fips,
        z=full_data2$Log_deaths_per_capita,
        text=full_data2$log_county_hover_text,
        hoverinfo = "text",
        colorscale="Viridis",
        colorbar = list(title = "Log Drug Overdose Deaths"),
        marker = list(line = list(width = 0)))

fig <- fig %>% layout(
    title = "Log Transformed Drug Overdose Deaths \n Per Capita in the US from 2020 to 2024",
    geo = g)
  
fig